unit ScannerListForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, NDevices, CheckLst;

type
  TScannerListForm = class(TForm)
    btnSelect: TButton;
    lbScanners: TCheckListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure btnSelectClick(Sender: TObject);
  private
    _scannerMan: TFPScannerMan;
    _initializedLibraries: String;
  public
    property ScannerMan: TFPScannerMan read _scannerMan write _scannerMan;
    property InitializedLibraries: String read _initializedLibraries write _initializedLibraries;
  end;

implementation

{$R *.dfm}

procedure Split
   (const Delimiter: Char;
    Input: string;
    const Strings: TStrings) ;
begin
   Assert(Assigned(Strings)) ;
   Strings.Clear;
   Strings.Delimiter := Delimiter;
   Strings.DelimitedText := Input;
end;

procedure TScannerListForm.FormCreate(Sender: TObject);
 var i: Integer;
     scanners: string;
     splitted: TStringList;
begin
 splitted := TStringList.Create;
 scanners := TFPScannerMan.GetAvailableScannerModules();
 if (Length(scanners) > 0) then
 begin
   try
     Split(';', scanners, splitted) ;
     lbScanners.Items.Clear;
     for i := 0 to splitted.Count - 1 do
     begin
        lbScanners.Items.Add(splitted[i]);
     end;
   finally
     splitted.Free;
   end;
 end
 else
 begin
   lbScanners.Items.Add('No scanner modules found.');
   lbScanners.Enabled := false;
 end;
end;

procedure TScannerListForm.FormActivate(Sender: TObject);
 var splitted: TStringList;
     i, index: Integer;
begin
 splitted := TStringList.Create;
 for i := 0 to lbScanners.Items.Count - 1 do
   lbScanners.Selected[i] := False;
 if (Length(InitializedLibraries) > 0) then
 begin
   try
     Split(';', InitializedLibraries, splitted) ;
     for i := 0 to splitted.Count - 1 do
     begin
        index := lbScanners.Items.IndexOf(splitted[i]);
        if (index >= 0) then
        begin
          lbScanners.Checked[index] := True;
        end;
     end;
   finally
     splitted.Free;
   end;
 end;
end;

procedure TScannerListForm.btnSelectClick(Sender: TObject);
 var temp: string;
     i: Integer;
begin
  for i := 0 to lbScanners.Items.Count - 1 do
  begin
    if (lbScanners.Checked[i]) then
    begin
      if (Length(temp) > 0) then
      begin
        temp := temp + ';';
      end;
      temp := temp + lbScanners.Items[i];
    end;
  end;
  InitializedLibraries := temp;

  self.Close;
end;

end.
